﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace IHISCETeim.SendSMSSvr
{
    /// <summary>
    /// 短信发送类
    /// </summary>
    public class SMSSendController
    {
        private static string neterror = "无法连接网络...请检查网络是否能访问";
        private static ISMS _SMSSupper = null;
        private static ISMS SMSSupper
        {
            get
            {
                try
                {
                    if (_SMSSupper == null)
                    {
                        string defaultsmssupper = ConfigurationManager.AppSettings["defaultsmssuppler"];
                        int p = defaultsmssupper.LastIndexOf('.');

                        string AssemblyName = defaultsmssupper.Substring(0, p);
                        string className = defaultsmssupper.Substring(p + 1, defaultsmssupper.Length - p - 1);
                        _SMSSupper = (ISMS)Assembly.Load(AssemblyName).CreateInstance(defaultsmssupper);
                    }
                }
                catch (Exception exc)
                {
                   Common.WriteLog(exc.Message+exc.StackTrace+exc.InnerException.StackTrace);
                    //Common.WriteLog(exc.InnerException.InnerException.StackTrace);
                }
                return _SMSSupper;
            }
        }
        /// <summary>
        /// 发送单条短信
        /// </summary>
        /// <param name="mobile">接收号码,多个可用,隔开</param>
        /// <param name="smsmessage">短信内容</param>
        /// <param name="stime">定时发送时间,如果有则是时间格式，如果无放空</param>
        /// <returns></returns>
        public static string SendSing(string mobile, string smsmessage, string stime)
        {
            if (SMSSupper != null)
                return SMSSupper.SingSend(mobile, smsmessage, stime);
            else
                return neterror;
        }

        /// <summary>
        /// 批量发送
        /// [同一内容发送到不同手机]
        /// </summary>
        /// <param name="mobile">多个手机号用,隔开</param>
        /// <param name="smsmessage"></param>
        /// <param name="stime">定时发送时间,如果有则是时间格式，如果无放空</param>
        /// <returns></returns>
        public static string BatchSend(string mobile, string smsmessage, string stime)
        {
            if (SMSSupper != null)
                return SMSSupper.BatchSend(mobile, smsmessage, stime);
            else
                return neterror;
        }
        /// <summary>
        /// 个性批量发送
        /// 一个手机号对应一个短信内容
        /// </summary>
        /// <param name="mobile">多个手机号用,隔开</param>
        /// <param name="smsmessage">多短信内容使用,隔开</param>
        /// <param name="stime">定时发送时间,如果有则是时间格式，如果无放空</param>
        /// <returns></returns>
        public static string PersonalitySend(string mobile, string smsmessage, string stime)
        {
            if (SMSSupper != null)
                return SMSSupper.PersonalitySend(mobile, smsmessage, stime);
            else
                return neterror;
        }
        /// <summary>
        /// 获取余额,显示还可发送多少条
        /// 如果查询成功，返回余额，反之返回失败消息
        /// </summary>
        /// <returns></returns>
        public static string GetBalance()
        {
            if (SMSSupper != null)
                return SMSSupper.GetBalance();
            else
                return neterror;
        }

    }
}
